package com.suikajy.damaiproject.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import com.suikajy.damaiproject.R;

import java.lang.ref.WeakReference;

/**
 * Created by suikajy on 2016/12/12.
 * 仿SwipeRefreshLayout的效果
 */

public class PullDownRefreshLayout extends RelativeLayout {

    private RefreshBar mRefreshBar;
    private View mPullDownTextView;
    public static final String RECEIVE_ACTION = "suikajy_pull_to_refresh";
    private RefreshReceiver mRefreshReceiver;
    private OnRefreshListener mRefreshListener;
    private Animation mTextInAnimation;
    private Animation mTextOutAnimation;
    private Animation mFadeInAnimation;
    private Animation mFadeOutAnimation;
    private float mStartY;
    private boolean isDragDown = false;

    public PullDownRefreshLayout(Context context) {
        this(context, null);
    }

    public PullDownRefreshLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullDownRefreshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mFadeInAnimation = AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_in);
        mFadeOutAnimation = AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_out);
        mTextInAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.push_in);
        mTextInAnimation.setFillAfter(true);
        mTextOutAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.push_out);
        mTextOutAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mPullDownTextView.setVisibility(INVISIBLE);
                mRefreshBar.setVisibility(GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mRefreshReceiver = new RefreshReceiver(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(RECEIVE_ACTION);
        getContext().registerReceiver(mRefreshReceiver, filter);
        Log.e("TAG", "onAttachedToWindow 注册广播");
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mRefreshReceiver != null) {
            getContext().unregisterReceiver(mRefreshReceiver);
            Log.e("TAG", "onDetachedFromWindow 注销广播");
        }
    }

    /**
     * 当刷新完成时停止PullDownRefreshLayout的刷新效果
     */
    public void stopRefresh() {
        mRefreshBar.setAnimation(null);
        mRefreshBar.stopRefresh();
        mRefreshBar.startAnimation(mFadeOutAnimation);
        mPullDownTextView.setAnimation(null);
        mPullDownTextView.startAnimation(mTextOutAnimation);
    }

    /**
     * 收到信号就开始刷新, 该方法被mRefreshReceiver调用
     */
    private void startRefresh() {
        if (isDragDown) {
            mRefreshBar.setAnimation(null);
            mRefreshBar.startRefresh();
            mRefreshBar.startAnimation(mFadeInAnimation);
            mRefreshBar.setVisibility(VISIBLE);
            mPullDownTextView.setVisibility(VISIBLE);
            mPullDownTextView.startAnimation(mTextInAnimation);
            if (mRefreshListener != null) {
                mRefreshListener.onRefresh();
            }
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isDragDown = false;
                mStartY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (!isDragDown) {
                    final float movedY = ev.getY();
                    if (movedY - mStartY > 3f) {
                        isDragDown = true;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                isDragDown = false;
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    public void setRefreshListener(OnRefreshListener mRefreshListener) {
        this.mRefreshListener = mRefreshListener;
    }

    public void setRefreshBar(RefreshBar mRefreshBar) {
        this.mRefreshBar = mRefreshBar;
    }

    public void setPullDownTextView(View mPullDownTextView) {
        this.mPullDownTextView = mPullDownTextView;
    }

    private static class RefreshReceiver extends BroadcastReceiver {

        private final WeakReference<PullDownRefreshLayout> reference;

        public RefreshReceiver(PullDownRefreshLayout pullDownRefreshLayout) {
            reference = new WeakReference<>(pullDownRefreshLayout);
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            PullDownRefreshLayout layout = reference.get();
            if (layout != null) {
                layout.startRefresh();
            }
        }
    }

    public interface OnRefreshListener {
        void onRefresh();
    }
}
